﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports System.Xml.Serialization
Namespace CMISObjects
   <XmlRoot("object", Namespace:=CmisNamespaceConstants.Cmisra)>
   Public Class CmisDocumentObject
      Inherits CMISObject
      Public Property IsLatestVersion As Boolean
      Public Property VersionSeriesCheckedOutId As String
      Public Property PwcParentId As String
      Public Property DisplayContentLink As Boolean = True
      Public Property CanGetAllVersions As Boolean = False
      <Runtime.Serialization.IgnoreDataMember()>
      Public Property Content As ContentContainer

    

      Public Sub New()
         MyBase.New()
      End Sub
      Public Sub New(id As String, properties() As CMISObjectModel.Properties.ICMISProperty, SingleTypeId As String, allowableActions As AllowableActions)
         Me.New(id, properties, SingleTypeId, allowableActions, True, String.Empty, String.Empty)
      End Sub
      ''' <summary>
      ''' 
      ''' </summary>
      ''' <param name="id"></param>
      ''' <param name="properties"></param>
      ''' <param name="SingleTypeId"></param>
      ''' <param name="allowableActions"></param>
      ''' <param name="isLatestVersion">Gibt an ob das Dokumente die aktuellste Version ist die Verfügbar ist.</param>
      ''' <param name="versionSeriesCheckedOutId">Optional: ID der Workingcopy, falls eine existiert.</param>
      ''' <param name="pwcParentId">Optional: Wenn das aktuelle Dokument eine Workingcopy ist muss hier die ID des Originaldokuments übergeben werden.</param>
      ''' <remarks></remarks>
      Public Sub New(id As String, properties() As CMISObjectModel.Properties.ICMISProperty, SingleTypeId As String, allowableActions As AllowableActions, isLatestVersion As Boolean, versionSeriesCheckedOutId As String, pwcParentId As String, Optional displayContentLink As Boolean = True, Optional canGetAllVersions As Boolean = False)
         MyBase.New(id, properties, SingleTypeId, allowableActions)
         Me.IsLatestVersion = isLatestVersion
         Me.VersionSeriesCheckedOutId = versionSeriesCheckedOutId
         Me.PwcParentId = pwcParentId
         Me.DisplayContentLink = displayContentLink
         Me.CanGetAllVersions = canGetAllVersions
      End Sub

      Public Overrides Function GetAtomLinks(baseUri As System.Uri, RepositoryInfo As CMISObjectModel.ServiceDocument.RepositoryInfo) As System.Collections.Generic.List(Of CMISLink)
         Dim retVal As List(Of CMISLink) = MyBase.GetAtomLinks(baseUri, RepositoryInfo)

         retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetObjectBase.Replace("{repositoryId}", RepositoryInfo.RepositoryId).Replace("{id}", Me.Id).TrimStart("/"c)), CmisAtomLinkConstants.Edit, CmisContentTypeConstants.Entry))
         retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetObjectParentsBase.Replace("{repositoryId}", RepositoryInfo.RepositoryId).Replace("{id}", Me.Id).TrimStart("/"c)), CmisAtomLinkConstants.Up, CmisContentTypeConstants.Feed))
         If Me.CanGetAllVersions Then
            retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetAllVersionsBase.Replace("{repositoryId}", RepositoryInfo.RepositoryId).TrimStart("/"c).Replace("{id}", Me.Id)), CmisAtomLinkConstants.VersionHistory, CmisContentTypeConstants.Feed))
         End If
         If Not IsLatestVersion AndAlso String.IsNullOrEmpty(PwcParentId) Then
            retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetObjectBase1.Replace("{returnVersion}", Common.GetDataMemberValueFromEnum(Enums.ReturnVersionEnum.latest, GetType(Enums.ReturnVersionEnum))).Replace("{repositoryId}", RepositoryInfo.RepositoryId).TrimStart("/"c).Replace("{id}", Me.Id)), CmisAtomLinkConstants.CurrentVersion, CmisContentTypeConstants.Entry))
         End If
         If RepositoryInfo.Capabilities.ContentStreamUpdatability <> ServiceDocument.RepositoryCapabilities.CapabilityContentStreamUpdates.None Then
            retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetContentStreamBase.Replace("{repositoryId}", RepositoryInfo.RepositoryId).Replace("{id}", Me.Id).TrimStart("/"c)), CmisAtomLinkConstants.EditMedia, CmisContentTypeConstants.Stream))
         End If
         If Not String.IsNullOrEmpty(VersionSeriesCheckedOutId) AndAlso String.IsNullOrEmpty(PwcParentId) Then
            retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetObjectBase2.Replace("{repositoryId}", RepositoryInfo.RepositoryId).TrimStart("/"c).Replace("{id}", Me.VersionSeriesCheckedOutId).Replace("{pwc}", "true")), CmisAtomLinkConstants.WorkingCopy, CmisContentTypeConstants.Entry))
         End If
         If Renditions IsNot Nothing AndAlso Renditions.Count > 0 Then
            retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetObjectBase.Replace("{repositoryId}", RepositoryInfo.RepositoryId).Replace("{id}", Me.Id).TrimStart("/"c)), CmisAtomLinkConstants.Alternate, Nothing))
         End If
         retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetObjectRelationshipsBase.Replace("{repositoryId}", RepositoryInfo.RepositoryId).TrimStart("/"c).Replace("{id}", Me.Id)), CmisAtomLinkConstants.Relationships, CmisContentTypeConstants.Feed))

         If Not String.IsNullOrEmpty(PwcParentId) Then
            retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetObjectBase.Replace("{repositoryId}", RepositoryInfo.RepositoryId).Replace("{id}", Me.PwcParentId).TrimStart("/"c)), CmisAtomLinkConstants.Via, CmisContentTypeConstants.Entry))
         End If

         Return retVal
      End Function
      Public Function GetContentLink(baseUri As Uri, RepositoryId As String) As Uri
         If Not DisplayContentLink Then
            Return Nothing
         End If
         Return New Uri(baseUri, ServiceURIs.GetContentStreamBase.Replace("{repositoryId}", RepositoryId).Replace("{id}", Me.Id).TrimStart("/"c))
      End Function
   End Class
End Namespace